package net.assassin8.bautista.stalker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class UserProfileActivity extends Activity {
	
	Context ctx;
	
	protected static final String TAG = "UserProfileActivity";
	private long mallId;

	private EditText firstNameInput;

	private EditText lastNameInput;

	private RadioButton maleInput;

	private RadioButton femaleInput;

	private EditText ageInput;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ctx = this;
		setContentView(R.layout.user_info);
		
		Bundle extras = getIntent().getExtras();
		
		mallId = extras.getLong("MALL_ID");
		
		Button saveButton = (Button) findViewById(R.id.SaveUserButton);
		firstNameInput = (EditText) findViewById(R.id.FirstNameEditText);
		lastNameInput = (EditText) findViewById(R.id.LastNameEditText);
		maleInput = (RadioButton) findViewById(R.id.MaleRadioButton);
		femaleInput = (RadioButton) findViewById(R.id.FemaleRadioButton);
		ageInput = (EditText) findViewById(R.id.AgeEditText);
		
		saveButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Save the user and go to SelectFloorActivity
				//@TODO: save the fields to the database
				StalkerProvider sp = new StalkerProvider(ctx);
				Editable firstName = firstNameInput.getText();
				Editable lastName = lastNameInput.getText();
				String gender = "";
				if(maleInput.isChecked()) {
					gender = "male";
				}
				if(femaleInput.isChecked()) {
					gender = "female";
				}
				Editable age = ageInput.getText();
				
				sp.open();
				long subjectId = sp.insertSubject(mallId, firstName, lastName, gender, age);
				long sessionId = sp.insertSession(subjectId);
				sp.close();
				
				Intent i = new Intent(UserProfileActivity.this, MallStalkerActivity.class);
				i.putExtra("MALL_ID", mallId);
				int floorNumber = 1;
				i.putExtra("FLOOR_NUMBER", floorNumber);
				i.putExtra("SESSION_ID", sessionId);
				Log.i(TAG, "Mall ID is: " + mallId);
				Log.i(TAG, "Floor number is: " + floorNumber);
				UserProfileActivity.this.startActivity(i);
				UserProfileActivity.this.finish();
			}
			
		});
	}
	
	@SuppressWarnings("unused")
	private OnClickListener maleListener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(femaleInput.isChecked()) {
				femaleInput.setChecked(false);
				maleInput.setChecked(true);
			}
		}
	};
	
	@SuppressWarnings("unused")
	private OnClickListener femaleListener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			if(maleInput.isChecked()) {
				femaleInput.setChecked(true);
				maleInput.setChecked(false);
			}
		}
	};

}
